﻿using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;
using Random = UnityEngine.Random;

namespace GameBasic
{
    public class RngList
    {
        public List<int> list;

        /// <summary>
        /// Get a list of randon value between the given <paramref name="range"/>
        /// </summary>
        /// <param name="count"></param>
        /// <param name="range"></param>
        /// <returns></returns>
        public List<int> GetRandom(int count, int2 range)
        {
            if (list == null)
                list = new List<int>(count);
            list.Clear();

            int requireCount = range.y - range.x + 1;
            count = math.clamp(count, 0, requireCount);

            if (count == 1)
            {
                list.Add(Random.Range(range.x, range.y + 1));
            }
            else
            {
                for (int i = range.x; i <= range.y; i++)
                    list.Add(i);

                // swap
                for (int i = 0; i < count; i++)
                {
                    int rng = Random.Range(0, count);
                    (list[rng], list[i]) = (list[i], list[rng]);
                }
                // remove
                list.RemoveRange(count, count - requireCount);
            }

            return list;
        }
    }
}